// Project: lmeter_14k50
// FileName: lmeter_14k50
#include <p18f14k50.h>
#include <delays.h>
#include <timers.h>
#pragma config CPUDIV = NOCLKDIV
#pragma config USBDIV = OFF
#pragma config FOSC = HS
#pragma config PLLEN = ON
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF
#pragma config BOREN = OFF
#pragma config BORV = 30
#pragma config WDTEN = OFF
#pragma config WDTPS = 32768
#pragma config MCLRE = OFF
#pragma config HFOFST = OFF
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma config BBSIZ = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CPB = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRTB = OFF
#pragma config WRTC = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTRB = OFF
#define lcd_DB LATB
#define lcd_E LATCbits.LATC7
#define lcd_RS LATCbits.LATC6
#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)
double L, F, FF;
unsigned long LL, FFF;
unsigned long A, TL;
unsigned char N;
char IND1[] = "L: ";
char IND2[] = " (uH)";
char frq[] = "f:";
char Hz[] = "(Hz)";
void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_init(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);
void lcd_putui(
unsigned long ui, unsigned char d);
void lcd_putf(
unsigned long f, unsigned char d);
void UserInit(void);
void lcd_Init(void);
void high(void);
void low(void);
#pragma interrupt high
#pragma interruptlow low
#pragma code H_vect = 0x0008
void H_isr(void){
_asm goto high _endasm
}
#pragma code L_vect = 0x0018
void L_isr(void){
_asm goto low _endasm
}
#pragma code
void high(){
if(INTCONbits.TMR0IF){
INTCONbits.TMR0IF = 0;
A++;
}
if(PIR1bits.TMR1IF){
PIR1bits.TMR1IF = 0;
N++;
if(N > 23){
N = 0;
TL = TMR0L;
FFF = A*256 + TL;
A = 0; TMR0L = 0;
F = FFF;
FF = F/1000000;
L = 1000000/(4*3.1416*3.1416*FF*FF*520)-14.36;
LL = L*10000;
TRISCbits.TRISC4 = 1;
}
TMR1H = 12; TMR1L = 12;
}
}
void low(){
}
void lcd_w4(char c){
lcd_DB = (c & 0xF0) | (lcd_DB & 0x0F);
lcd_E = 1;
Delay10TCYx(20);
lcd_E = 0;
}
void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}
void lcd_clr(void){
lcd_c4(0x01);
}
void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}
void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}
void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}
void lcd_putui(
unsigned long ui, unsigned char d){
unsigned char i;
unsigned char buf[10];
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (ui % 10) + '0';
ui = ui / 10;
i--;
} while(ui > 0);
lcd_putchr(buf[4]);
lcd_putchr(buf[5]);
lcd_putchr('.');
lcd_putchr(buf[6]);
lcd_putchr(buf[7]);
}
void lcd_putf(
unsigned long f, unsigned char d){
unsigned char i;
unsigned char buf[10];
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
for(i=(10-d); i<10; i++) lcd_putchr(buf[i]);
}
void lcd_init(void){
Delay_20mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x06);
lcd_c4(0x01);
}
void UserInit(void){
N = 0; A = 0;
}
void main(void){
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b00110000;
ANSEL = 0b00000000;
ANSELH = 0b00000000;
OpenTimer0(
TIMER_INT_ON &
T0_8BIT &
T0_SOURCE_EXT &
T0_PS_1_1 &
T0_EDGE_RISE
);
INTCON2bits.TMR0IP = 1;
INTCONbits.TMR0IF = 0;
OpenTimer1(
TIMER_INT_ON &
T1_8BIT_RW &
T1_SOURCE_INT &
T1_PS_1_8 &
T1_OSC1EN_OFF &
T1_SYNC_EXT_OFF
);
IPR1bits.TMR1IP = 1;
PIR1bits.TMR1IF = 0;
RCONbits.IPEN = 1;
INTCONbits.GIEH = 1;
INTCONbits.GIEL = 1;
TMR0H = 0; TMR0L = 0;
TMR1L = 10;
UserInit();
lcd_init();
while(1){
lcd_clr();
lcd_locate(0, 1);
lcd_puts(IND1);
lcd_putui(LL, 6);
lcd_puts(IND2);
lcd_locate(1, 1);
lcd_puts(frq);
lcd_putf(FFF, 8);
lcd_puts(Hz);
Delay_100mS;
}
}